package com.chatifyme;


import java.util.ArrayList;
import java.util.Collection;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;


public class UsersListActivity extends BaseActivity  { 
	XMPPConnection xConn;
	ListView lv1;
	ArrayList<UserDetail> arrEnt = new ArrayList<UserDetail>();
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//xConn=NetworkConnection.connection;
	setContentView(R.layout.activity_users);
		
		UserList();
	}
	
	public void UserList(){
		Roster roster = xConn.getRoster();
		Collection<RosterEntry> entries = roster.getEntries();
		
		UserDetail userItem;
		for (RosterEntry entry : entries) {
			userItem=new UserDetail();
			userItem.Name=entry.getUser();
			Presence presence = roster.getPresence(entry.getUser());
			userItem.Status=presence.getStatus();
			if (presence.isAvailable()) {
				userItem.Presence="online";   
			}else if (presence.isAway()) {
				userItem.Presence="away";
			}
			else{
				userItem.Presence="offline";
			}
			
			arrEnt.add(userItem);
		}
		
		
		lv1 =(ListView) findViewById(R.id.list);
		UserListAdapter adapter = new UserListAdapter(UsersListActivity.this, arrEnt);
		
        lv1.setAdapter(adapter);
     	
     	lv1.setOnItemClickListener(new OnItemClickListener()
      	{
      	public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
      	{
      		Intent intent=new Intent(UsersListActivity.this,ChatActivity.class) ;
      		
      		String uName=arrEnt.get(position).Name.toString();
         	intent.putExtra("Recipient", uName);
         	startActivity(intent);
         	
      	}
      	});
      	 	
	}
	@Override
    public void onDestroy()
    {
        lv1.setAdapter(null);
        super.onDestroy();
    }
}
